define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/coupon/index' + location.search,
                    add_url: 'project/coupon/add',
                    edit_url: 'project/coupon/edit',
                    del_url: 'project/coupon/del',
                    table: 'coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true,},
                        { field: 'id', title: __('Id'), sortable: false },
                        { field: 'title', title: __('title'), operate: 'LIKE' },
                        { field: 'content', title: __('content') , operate: false},
                        { field: 'requirement', title: __('requirement') , operate: false},
                        { field: 'discount', title: __('discount') , operate: false},
                        { field: 'nums', title: __('nums') , operate: false},
                        { field: 'status', title: __('status'), searchList: { "0": __('已失效'), "1": __('有效')}, formatter: Table.api.formatter.status },
                        { field: 'createtime', title: __('createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'endtime', title: __('endtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                // 查看领取优惠劵详情
                                {
                                    name: 'receive', text: '查看领取',
                                    title: '查看领取详情',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-bars',
                                    url: 'project/coupon/receive',
                                    extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                },
                            ]
                        }
                    ]
                ],
                escape: false,
                // search:false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        receive: function () {
            Table.api.init({
                extend: {
                    index_url: 'project/coupon/receive?ids=' + Fast.api.query('ids'),
                    table: 'coupon_receive',
                }
            });
            // console.log(business.id);
            var receiveTable = $("#table");
            receiveTable.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'receive.id',
                search: false,
                // 关闭导出
                showExport: false,
                // 关闭显示的列
                showColumns: false,
                // 关闭切换显示
                showToggle: false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { field: 'id', title: __('Id'), searchable: true },
                        { field: 'business.nickname', title: __('Rbusiness'), operate: 'LIKE' },
                        { field: 'status', title: __('status'), searchList: { "0": __('未使用'), "1": __('已使用'), "2": __('已失效')}, formatter: Table.api.formatter.status },
                        { field: 'createtime', title: __('Rcreatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },                    
                        { field: 'updatetime', title: __('Rupdatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },                    
                    ]
                ],
                escape: false,
            });
           
            // 为表格绑定事件
            Table.api.bindevent(receiveTable);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
    };
    return Controller;
});